package GUILayer;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.* ;
import java.util.*;
import ControlLayer.*;

/**
 * Write a description of class UIDVD here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UIDVD
{

     private DVDCtr dvdController;
     
     private  JTextField idText = new JTextField(10);
     private  JTextField idDeleteDVDText = new JTextField(10);
     private  JTextField titleText = new JTextField(10);
     private  JTextField artistText = new JTextField(10);
     private  JTextField publicationDateText=new JTextField(10);
     private  JTextField serialCopyText = new JTextField(10);
     private  JTextField purchaseDateText = new JTextField(10);
     private  JTextField purchasePriceText=new JTextField(10);
       
       
    /**
     * Constructor for objects of class UIDVD
     */
    public UIDVD()
    {
       dvdController=new DVDCtr();
    }
    
    
    public JPanel getDVDsPanel(JFrame frame)
   {
       
        JPanel panelMain=new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
        
        
        JButton addDVD = new JButton("Add DVD");
        JButton deleteDVD = new JButton("Delete DVD");
        JButton showDVD = new JButton("Show DVD");
        JButton showAllDVDs = new JButton("Show All DVDs");
        
        final JFrame frame1=frame;
        addDVD.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { addDVD(frame1); }
                           }); 
                                              
        deleteDVD.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { deleteDVD(frame1); }
                           }); 
       showDVD.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) { showDVD(frame1); }
                           }); 
                           
       showAllDVDs.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) { showAllDVDs(frame1); }
                           }); 
         
        JLabel idLabel = new JLabel("ID");
        JLabel idDeleteDVDLabel = new JLabel("ID");
        JLabel titleLabel=new JLabel ("Title");
        JLabel artistLabel=new JLabel ("Artist");
        JLabel publicationDateLabel=new JLabel ("Publication Date");
        
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        addComponent(addDVD,panelMain);
        
     
        JPanel panelID=new JPanel();
        panelID.add(idLabel);
        panelID.add(idText);
        addComponent(panelID,panelMain);
        
        JPanel panelTitle=new JPanel();
        panelTitle.add(titleLabel);
        panelTitle.add(titleText);
        addComponent(panelTitle,panelMain);
        
       
        
        JPanel panelArtist=new JPanel();
        panelArtist.add(artistLabel);
        panelArtist.add(artistText);
        addComponent(panelArtist,panelMain);
        
        
        JPanel panelPublicationDate=new JPanel();
        panelPublicationDate.add(publicationDateLabel);
        panelPublicationDate.add(publicationDateText);
        addComponent(panelPublicationDate,panelMain);
        
         addComponent(addDVD,panelMain);
         panelMain.add(Box.createRigidArea(new Dimension(300,25)));
        
        
         
        JPanel panelDeleteDVD=new JPanel();
        panelDeleteDVD.add(idDeleteDVDLabel);
        panelDeleteDVD.add(idDeleteDVDText);
        addComponent(panelDeleteDVD,panelMain);
        
       JPanel panelOptionButtons=new JPanel(); 
       panelOptionButtons.add(deleteDVD);
       panelOptionButtons.add(showDVD);
        addComponent(panelOptionButtons,panelMain);;
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
      
        
        addComponent(showAllDVDs,panelMain);
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
        
        
        return panelMain;
    
    
    }
    
    
    private void addDVD(JFrame frame)
    {
        try
          {
           long id=Long.valueOf(idText.getText()) ;
    
          dvdController.createDVD(id,titleText.getText(),artistText.getText(),
           publicationDateText.getText());
           idText.setText("");
           titleText.setText("");
           artistText.setText("");
           publicationDateText.setText("");
       
          
          JOptionPane.showMessageDialog(frame,"DVD added successfuly.");
          }
          
          catch(Exception e)
          {
            JOptionPane.showMessageDialog(frame,"Error while adding DVD,please try again!.","",JOptionPane.ERROR_MESSAGE);
            }
    
    }
    
    
    private void deleteDVD(JFrame frame)
    {
        try
        {
         long  id=Long.valueOf(idDeleteDVDText.getText()) ;

         int choice=JOptionPane.showConfirmDialog(frame,"Do you really want to delete this DVD?", "", JOptionPane.YES_NO_OPTION);
           if(choice==0)
           {
           dvdController.deleteDVD(id);
           idDeleteDVDText.setText("");
           JOptionPane.showMessageDialog(frame,"DVD deleted successfuly.");
           }
        }
        
        catch(Exception e)
        {
          JOptionPane.showMessageDialog(frame,"Wrong ID,please try again!.","",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    
   private void showDVD(JFrame frame)
    {
       
        try
        {
        long  id=Long.valueOf(idDeleteDVDText.getText());  
        String[] columnNames = {"ID","Title","Artist","Publication Date"};
        Object [][] data={ {id,dvdController.getDVDTitle(id),dvdController.getDVDArtist(id)
            ,dvdController.getDVDPublicationDate(id)}};
   
          idDeleteDVDText.setText("");  
        JTable table = new JTable(data, columnNames);
        JFrame inFrame=new JFrame();
        inFrame.setLayout(new BorderLayout());
        inFrame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        inFrame.add(table, BorderLayout.CENTER);
        
        inFrame.pack();
        inFrame.setVisible(true);
       }
       
       
       
       catch(Exception e)
       {
        JOptionPane.showMessageDialog(frame,"Wrong ID,please try again!.","",JOptionPane.ERROR_MESSAGE);
       }

    }
    
    private void showAllDVDs(JFrame frame)
    {
     String[] columnNames = {"ID","Title","Artist","Publication Date"};
     ArrayList<Long> ids=dvdController.getAllIDs();
     ArrayList<String> titles=dvdController. getAllTitles();
     ArrayList<String> artists=dvdController.getAllArtists();
     ArrayList<String> publicationDates=dvdController.getPublicationDates();

     Object [][] data=new Object [ids.size()][6];
     
        for(int i=0;i<ids.size();i++)
            {
            data[i][0]=ids.get(i);
            data[i][1]=titles.get(i);
            data[i][2]=artists.get(i);
            data[i][3]=publicationDates.get(i);
   
            }
            
        JTable table = new JTable(data, columnNames);
        JFrame inFrame=new JFrame();
        inFrame.setLayout(new BorderLayout());
        inFrame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        inFrame.add(table, BorderLayout.CENTER);
        
        inFrame.pack();
        inFrame.setVisible(true);
    }    
    
         private static void addComponent(JComponent component, Container container)
    {
        component.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(component);
        
    }


}
